package day06;

public class CoffeeDecoratorExample {

    public static void main(String[] args) {
        // 创建一个简单的咖啡
        Coffee coffee = new SimpleCoffee();
        System.out.println("描述: " + coffee.getDescription() + " | 价格: $" + coffee.getCost());

        // 使用 MilkDecorator 装饰咖啡
        coffee = new MilkDecorator(coffee);
        System.out.println("描述: " + coffee.getDescription() + " | 价格: $" + coffee.getCost());

        // 在有牛奶的咖啡基础上，再使用 SugarDecorator 装饰
        coffee = new SugarDecorator(coffee);
        System.out.println("描述: " + coffee.getDescription() + " | 价格: $" + coffee.getCost());
    }

    // 抽象组件：Coffee 接口
    public interface Coffee {
        String getDescription();

        double getCost();
    }

    // 抽象装饰者：CoffeeDecorator 抽象类
    public abstract static class CoffeeDecorator implements Coffee {
        protected Coffee decoratedCoffee;

        public CoffeeDecorator(Coffee decoratedCoffee) {
            this.decoratedCoffee = decoratedCoffee;
        }

        @Override
        public String getDescription() {
            return decoratedCoffee.getDescription();
        }

        @Override
        public double getCost() {
            return decoratedCoffee.getCost();
        }
    }

    // 具体组件：SimpleCoffee 类
    public static class SimpleCoffee implements Coffee {
        @Override
        public String getDescription() {
            return "Simple Coffee";
        }

        @Override
        public double getCost() {
            return 2.0;
        }
    }

    // MilkDecorator 类
    public static class MilkDecorator extends CoffeeDecorator {
        public MilkDecorator(Coffee decoratedCoffee) {
            super(decoratedCoffee);
        }

        @Override
        public String getDescription() {
            return super.getDescription() + ", Milk";
        }

        @Override
        public double getCost() {
            return super.getCost() + 0.5;
        }
    }

    // SugarDecorator 类
    public static class SugarDecorator extends CoffeeDecorator {
        public SugarDecorator(Coffee decoratedCoffee) {
            super(decoratedCoffee);
        }

        @Override
        public String getDescription() {
            return super.getDescription() + ", Sugar";
        }

        @Override
        public double getCost() {
            return super.getCost() + 0.25;
        }
    }

}